package cn.tedu.sp02.controller;

import cn.tedu.sp01.entity.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(
            @PathVariable String orderId) throws InterruptedException {
              List<Item> items = itemService.getItems(orderId);
        if (Math.random() <0.9) {
            int i = new Random().nextInt(5000);
            System.out.println("延迟"+i);
            Thread.sleep(i);
        }
        return JsonResult.ok().data(items);
    }

    @PostMapping("/decreaseNumber")
    public JsonResult<?> addScore(
            @RequestBody List<Item> items
    ) {
        itemService.decreaseNumber(items);
        return JsonResult.ok();
    }
    @GetMapping("/favicon.ico")
    public void ico(){

    }
}
